package org.linlinjava.litemall.wx.web;

import static org.linlinjava.litemall.wx.util.WxResponseCode.COMMON_DETAIL_IS_NULL;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.constant.DBConmonConstant;
import org.linlinjava.litemall.db.service.LitemallHerbService;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.service.GetRegionService;
import org.linlinjava.litemall.wx.service.WxHerbService;
import org.linlinjava.litemall.wx.service.WxQualificationsService;
import org.linlinjava.litemall.wx.vo.QualificationsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;

/**
 * 药品品类服务
 */
@RestController
@RequestMapping("/wx/herb")
@Validated
public class WxHerbController extends GetRegionService {
	private final Log logger = LogFactory.getLog(WxHerbController.class);

//	@Autowired
//	private LitemallHerbService herbService;

	@Autowired
	private WxHerbService wxHerbService;

	@Autowired
	private WxQualificationsService qualificationsService;

	/**
	 * 供选择的药品品类通用名列表
	 * 
	 * @param userId
	 * @return
	 */
	@GetMapping("selectGenericNameList")
	public Object selectGenericNameList(@LoginUser Integer userId, 
			@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer limit,
			@RequestParam(defaultValue = "") String sort,
			@RequestParam(defaultValue = "desc") String order,
			@RequestParam(defaultValue = "") String searchValue) {
		return wxHerbService.selectGenericNameList(userId, page, limit, sort, order, searchValue);
	}
	
//	@GetMapping("selectAuthGenericNameList")
//	public Object selectAuthGenericNameList(@LoginUser Integer userId, 
//			@RequestParam(defaultValue = "1") Integer page,
//			@RequestParam(defaultValue = "10") Integer limit,
//			@RequestParam(defaultValue = "") String sort,
//			@RequestParam(defaultValue = "desc") String order,
//			@RequestParam(defaultValue = "") String searchValue) {
//		return wxHerbService.selectAuthGenericNameList(userId, page, limit, sort, order, searchValue);
//	}

	@GetMapping("selectAuthGenericNameList")
	public Object selectAuthGenericNameList(@LoginUser Integer userId, 
			@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer limit,
			@RequestParam(defaultValue = "") String sort,
			@RequestParam(defaultValue = "desc") String order,
			@RequestParam(defaultValue = "") String searchValue) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}
		QualificationsVo qualificationsVo = qualificationsService.getQualificationsVo(userId);
		if (qualificationsVo == null) {
			return ResponseUtil.fail(COMMON_DETAIL_IS_NULL, "未获取到资质信息，请刷新重试");
		}

		Object data = null;
		switch (DBConmonConstant.UserType.getEnumByValue(qualificationsVo.getUserType())) {
		case Personal:
			data = wxHerbService.selectAuthGenericNameList(userId, page, limit, sort, order, searchValue);
			break;
		case Manufactor:
		case Distributor:
		default:
			data = wxHerbService.selectGenericNameDescribeList(userId, page, limit, sort, order, searchValue);
			break;
		}
		
		return data;
	}

	/**
	 * 供选择的药品品类列表
	 * 
	 * @param userId
	 * @return
	 */
//	@GetMapping("selectList")
//	public Object selectList(@LoginUser Integer userId, @RequestParam(defaultValue = "") String searchContent,
//			@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit) {
//		if (userId == null) {
//			return ResponseUtil.unlogin();
//		}
//		Map<String, Object> data = herbService.getAllHerbVoList(searchContent, page, limit);
//		return ResponseUtil.ok(data);
//	}
//
//	@GetMapping("authSelectList")
//	public Object authSelectList(@LoginUser Integer userId, @RequestParam(defaultValue = "") String searchContent,
//			@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit) {
//		if (userId == null) {
//			return ResponseUtil.unlogin();
//		}
//		QualificationsVo qualificationsVo = qualificationsService.getQualificationsVo(userId);
//		if (qualificationsVo == null) {
//			return ResponseUtil.fail(COMMON_DETAIL_IS_NULL, "未获取到资质信息，请刷新重试");
//		}
//
//		Map<String, Object> data = null;
//		switch (DBConmonConstant.UserType.getEnumByValue(qualificationsVo.getUserType())) {
//		case Personal:
//			data = wxHerbService.getAuthHerbVoList(searchContent, page, limit, userId);
//			break;
//		case Manufactor:
//		case Distributor:
//		default:
//			data = herbService.getAllHerbVoList(searchContent, page, limit);
//			break;
//		}
//
//		if (data == null) {
//			data = Maps.newHashMap();
//		}
//		return ResponseUtil.ok(data);
//	}

}